package com.jacobson.huobi.mvp

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.widget.Toast
import com.alibaba.fastjson.JSONObject
import com.jacobson.core.base.Immersive
import com.jacobson.core.di.components.AppComponent
import com.jacobson.core.di.modules.ActivityModule
import com.jacobson.core.mvp.BaseMvpActivity
import com.jacobson.huobi.Constants
import com.jacobson.huobi.R
import com.jacobson.huobi.di.components.DaggerInviteFriendsActivityComponent
import com.jacobson.huobi.di.modules.InviteFriendsActivityModule
import com.jacobson.huobi.entities.response.CommonResponse
import com.jacobson.huobi.entities.vo.LoginRespVo
import com.jacobson.huobi.mvp.contract.InviteFriendsContract
import com.jacobson.huobi.utils.ResourcesUtil
import com.jacobson.huobi.utils.TokenStorageUtil
import kotlinx.android.synthetic.main.activity_invite_friends.*
import kotlinx.android.synthetic.main.activity_kyc_auth.btnBack
import kotlinx.android.synthetic.main.fragment_currency.*

@Immersive(fitSystemWindows = false)
class InviteFriendsActivity :
    BaseMvpActivity<InviteFriendsContract.Presenter>(), InviteFriendsContract.View {

    private var loginRespVo: LoginRespVo? = TokenStorageUtil.read()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //修改底部功能键背景色 只支持api为21及以上
        if (Build.VERSION.SDK_INT  >= Build.VERSION_CODES.LOLLIPOP) {
            window.navigationBarColor = ResourcesUtil.getColor(R.color.colorPrimary) //设定底部导航栏  同全局颜色
        }
    }
    override fun setupActivityComponent(appComponent: AppComponent) {
        DaggerInviteFriendsActivityComponent.builder()
            .inviteFriendsActivityModule(InviteFriendsActivityModule(this))
            .appComponent(appComponent)
            .activityModule(ActivityModule(this))
            .build()
            .inject(this)
    }

    override fun initView(savedInstanceState: Bundle?): Int = R.layout.activity_invite_friends

    override fun initData(savedInstanceState: Bundle?) {
        initBtnClick()

        presenter.loadSharedInfo()
    }

    /**
     * 查询返佣信息
     */
    override fun onSharedInfoResultResp(response: CommonResponse<JSONObject>) {
        if (response.code == Constants.RESP_OK) {
            var data = response.data
//            tvTotal.text = data.getInteger("total").toString()//分享总人数
            tvTotal.text = data.getInteger("oneLevelTotal").toString()//一级分享总人数
            tvSetTlementTotal.text = data.getInteger("settlement_total").toString()//已结算人数
            tvRechargeTotal.text = data.getInteger("recharge_total").toString()//已充值人数（审核通过）
            tvKycTotal.text = data.getInteger("kyc_total").toString()//已认证人数（审核通过）
            tvCommission.text = data.getString("commission")//已结算金额
        }else {
            Toast.makeText(this, "查询返佣信息失败", Toast.LENGTH_SHORT).show()
        }
    }

    override fun onError(type: Int, extra: Any?, code: Int, message: String): Boolean {
        smartRefreshLayout.finishLoadMore()
        smartRefreshLayout.finishRefresh()
        logger.e("error >>> $message")
        return super.onError(type, extra, code, message)
    }

    private fun initBtnClick() {
        btnBack.setOnClickListener {
            this.finish()
        }

        btnCopyShareUrl.setOnClickListener {
            if (loginRespVo == null) {
                Toast.makeText(this, "获取用户信息失败,请重新登录", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            var clipboardManager = this.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
            var clip = ClipData.newPlainText("text", Constants.APP_SHARE_URL + "?sharedId=" +loginRespVo!!.userId)
            clipboardManager.primaryClip = clip
            Toast.makeText(this, "邀请链接复制成功", Toast.LENGTH_SHORT).show()
        }

        btnInviteDetails.setOnClickListener {
            InviteUserListActivity.startActivity(this)
        }
    }

    companion object {
        fun startActivity(context: Context) {
            Intent(context, InviteFriendsActivity::class.java).run {
                context.startActivity(this)
            }
        }

    }

}
